package com.hsq.androidproject.ui;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.adapter.MyFragmentPagerAdapter;
import com.hsq.androidproject.db.DBHelper;
import com.hsq.androidproject.entity.bd_entity.Follow;
import com.hsq.androidproject.utils.glide.GlideCircleTransform;

import java.util.ArrayList;

public class ShowHotStartsActivity extends FragmentActivity {
    private ImageView iv_background;         /////背景
    private TextView tv_name;             ////名字
    private ImageView iv_thumb;        ////头像
    private TextView tv_fans;           ////粉丝
    private TextView tv_rank;           ////排名
    private Button btn_follow;      ////是否关注

    private DBHelper dbHelper;          ///////数据库 是否关注
    private String name;
    private String thumb;
    private int sid;
    private int rank;
    private int fansCount;
    private String baileUrl;

    String usersName = "peng";           ////用来判断是否登录


    private ViewPager mPager;
    private TextView tv_new, tv_zuopin, tv_fans1;
    Resources resources;
    private int currIndex = 0;
    private ArrayList<Fragment> fragmentsList;

    QXzpFragment zpj;//作品集
    QXzxFragment zxdt;//最新动态
    QXfsFragment fs;//粉丝区
    private TextView barText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_hot_starts);
        initView();
        resources = getResources();
        InitViewPager();
        InitTextView();
        InitTextBar();

        Follow follow = new Follow(name, usersName);
        if (dbHelper.isExistF(follow)) {             //////已经关注
            btn_follow.setText("已关注");
        } else {                                        ///////没有关注
            btn_follow.setText("+关注");
        }
    }

    private void InitTextBar() {
        barText = (TextView) findViewById(R.id.cursor);
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        // 得到显示屏宽度
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        // 1/3屏幕宽度
        int tabLineLength = metrics.widthPixels / 3;
        LayoutParams lp = (LayoutParams) barText.getLayoutParams();
        lp.width = tabLineLength;
        barText.setLayoutParams(lp);
    }

    private void initView() {

        dbHelper = new DBHelper(ShowHotStartsActivity.this);

        iv_background = (ImageView) findViewById(R.id.iv_background);
        btn_follow = (Button) findViewById(R.id.btn_follow);
        tv_name = (TextView) findViewById(R.id.tv_name);
        iv_thumb = (ImageView) findViewById(R.id.iv_thumb);
        tv_fans = (TextView) findViewById(R.id.tv_fans);
        tv_rank = (TextView) findViewById(R.id.tv_rank);

        Intent intent = getIntent();        ///////MyGridViewAdapter传输的数据       MyListViewStarRankingAdapter
        name = intent.getStringExtra("name");
        baileUrl = intent.getStringExtra("baileUrl");  ///// 跳转到WebView 百度百科
        thumb = intent.getStringExtra("thumb");
        sid = intent.getIntExtra("sid", 0);  ////sid   /////http://api.hanju.koudaibaobao.com/api/star/videos?sid=3&sort=t&offset=0&count=201
        rank = intent.getIntExtra("rank", 0);
        fansCount = intent.getIntExtra("fansCount", 0);

        tv_name.setText(name + "          ");
        Glide.with(this)
                .load(thumb)
                .transform(new GlideCircleTransform(this))//圆形
                .into(iv_thumb);
        tv_fans.setText("粉丝   " + fansCount);
        tv_rank.setText("排名   第" + rank + "名");

        /*Glide.with(this)                      /////背景
                .load(thumb)
                .into(iv_background);*/
    }

    private void InitTextView() {
        tv_new = (TextView) findViewById(R.id.tv_new);
        tv_zuopin = (TextView) findViewById(R.id.tv_zuopin);
        tv_fans1 = (TextView) findViewById(R.id.tv_fans1);

        tv_new.setOnClickListener(new MyOnClickListener(0));
        tv_zuopin.setOnClickListener(new MyOnClickListener(1));
        tv_fans1.setOnClickListener(new MyOnClickListener(2));

        tv_new.setTextColor(Color.parseColor("#ff5694"));
        tv_zuopin.setTextColor(resources.getColor(R.color.txt_black));
        tv_fans1.setTextColor(resources.getColor(R.color.txt_black));
    }

    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mPager.setCurrentItem(index);
        }
    }


    /**
     * 初始化ViewPager
     */

    private void InitViewPager() {
        mPager = (ViewPager) findViewById(R.id.vPager);
        fragmentsList = new ArrayList<>();

        zxdt = new QXzxFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("sid", sid);
        zxdt.setArguments(bundle);

        zpj = new QXzpFragment();
        zpj.setArguments(bundle);

        fs = new QXfsFragment();
        fs.setArguments(bundle);

        fragmentsList.add(zxdt);
        fragmentsList.add(zpj);
        fragmentsList.add(fs);

        //给ViewPager设置适配器
        mPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList));
        mPager.setOnPageChangeListener(new MyOnPageChangeListener());//页面变化时的监听器
        mPager.setCurrentItem(0);//设置当前显示标签页为第一页
    }

    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageSelected(int arg0) {
            currIndex = arg0;
            int i = currIndex + 1;
            if (i == 1) {
                tv_new.setTextColor(Color.parseColor("#ff5694"));
                tv_zuopin.setTextColor(resources.getColor(R.color.txt_black));
                tv_fans1.setTextColor(resources.getColor(R.color.txt_black));
            } else if (i == 2) {
                tv_new.setTextColor(resources.getColor(R.color.txt_black));
                tv_zuopin.setTextColor(Color.parseColor("#ff5694"));
                tv_fans1.setTextColor(resources.getColor(R.color.txt_black));
            } else if (i == 3) {
                tv_new.setTextColor(resources.getColor(R.color.txt_black));
                tv_zuopin.setTextColor(resources.getColor(R.color.txt_black));
                tv_fans1.setTextColor(Color.parseColor("#ff5694"));
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            //取得该控件的实例
            LayoutParams ll = (LayoutParams) barText.getLayoutParams();

            if (currIndex == arg0) {
                ll.leftMargin = (int) (currIndex * barText.getWidth() + arg1
                        * barText.getWidth());
            } else if (currIndex > arg0) {
                ll.leftMargin = (int) (currIndex * barText.getWidth() - (1 - arg1) * barText.getWidth());
            }
            barText.setLayoutParams(ll);
        }

    }

    public void click_back(View view) {         /////返回上一个页面
        finish();
    }

    public void click_web(View view) {      ///根据 详细资料   baileUrl
        Intent intents = new Intent(this, ShowWebActivity.class);
        intents.putExtra("name", name);
        intents.putExtra("baiduUrl", baileUrl);
        startActivity(intents);
    }

    public void click_guanzhu(View view) {      /////是否关注
        Follow follow = new Follow(name, usersName);
        if (dbHelper.isExistF(follow)) {               ////
            dbHelper.deleteFollowByNameAndUserName(follow);
            btn_follow.setText("+关注");
        } else {                                     ////
            Follow collect = new Follow(name, usersName, baileUrl, thumb, sid, rank, fansCount);
            dbHelper.insert(collect);
            btn_follow.setText("已关注");
        }
    }
}
